package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.enums.ordercenter.ForecastOrderTypeEnum;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.epoch.app.crforecastsales.enums.ForecastStatusEnum;
import com.epoch.app.crforecastsales.forecastorderlineindex.model.ForecastOrderLineIndex;
import com.epoch.app.mnordercenter.model.dto.ForecastOrderLineSDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;

/**
 * @author zhoubc
 * @date 2023/5/31 14:53
 */
public class ForecastOrderLineIndexToForecastOrderLineSDOConvertor {

    public static ForecastOrderLineSDO convert(ForecastOrderLineIndex forecastOrderLine) {
        ForecastOrderLineSDO forecastOrderLineSDO = new ForecastOrderLineSDO();
        forecastOrderLineSDO.setId(forecastOrderLine.getSubId()!=null?String.valueOf(forecastOrderLine.getSubId()):null);
        forecastOrderLineSDO.setOrderId(forecastOrderLine.getMainId()!=null?String.valueOf(forecastOrderLine.getMainId()):null);
        forecastOrderLineSDO.setOrderBizId(forecastOrderLine.getMainOrderBizId());
        forecastOrderLineSDO.setOuterOrderId(forecastOrderLine.getMainOuterOrderId());
        forecastOrderLineSDO.setOrderType(forecastOrderLine.getMainOrderTypeCode());
        forecastOrderLineSDO.setOrderTypeName(forecastOrderLine.getMainOrderTypeName());
        forecastOrderLineSDO.setOrderLineNo(forecastOrderLine.getSubSerialNumber());
        forecastOrderLineSDO.setOuterOrderLineId(forecastOrderLine.getSubOuterLineId());
        forecastOrderLineSDO.setOrderLineTag(forecastOrderLine.getMainOrderTag());
        forecastOrderLineSDO.setScItemId(forecastOrderLine.getSubScItemId());
        forecastOrderLineSDO.setScItemCode(forecastOrderLine.getSubScItemOutCode());
        forecastOrderLineSDO.setScItemTitle(forecastOrderLine.getSubScItemTitle());
        forecastOrderLineSDO.setProductGroupCode(forecastOrderLine.getMainProductGroupCode());
        forecastOrderLineSDO.setProductGroupName(forecastOrderLine.getMainProductGroupName());
        forecastOrderLineSDO.setCustomerCode(forecastOrderLine.getMainSoldToPartyCode());
        forecastOrderLineSDO.setCustomerName(forecastOrderLine.getMainSoldToPartyName());
        forecastOrderLineSDO.setReceiveAddressCode(forecastOrderLine.getMainShipToPartyCode());
        forecastOrderLineSDO.setReceiveDetailAddress(forecastOrderLine.getMainShipToPartyName());
        forecastOrderLineSDO.setBisUnitCode(forecastOrderLine.getMainBusinessUnitCode());
        forecastOrderLineSDO.setBisUnitName(forecastOrderLine.getMainBusinessUnitName());
        forecastOrderLineSDO.setSalesOrganizationCode(forecastOrderLine.getMainSalesOrganizationCode());
        forecastOrderLineSDO.setSalesOrganizationName(forecastOrderLine.getMainSalesOrganizationName());
        forecastOrderLineSDO.setSalesChannelCode(forecastOrderLine.getMainSaleChannelCode());
        forecastOrderLineSDO.setSalesChannelName(forecastOrderLine.getMainSaleChannelName());
        forecastOrderLineSDO.setSalesDepartmentCode(forecastOrderLine.getMainSalesDepartmentCode());
        forecastOrderLineSDO.setSalesDepartmentName(forecastOrderLine.getMainSalesDepartmentName());
        forecastOrderLineSDO.setChannelCode(forecastOrderLine.getMainChannelCode());
        forecastOrderLineSDO.setChannelName(forecastOrderLine.getMainChannelName());
        forecastOrderLineSDO.setBusinessType(forecastOrderLine.getMainBusinessType());
        forecastOrderLineSDO.setBusinessTypeName(forecastOrderLine.getMainBusinessTypeName());

        if (forecastOrderLine.getSubStatus() != null) {
            ForecastStatusEnum forecastStatusEnum = ForecastStatusEnum.of(Integer.parseInt(String.valueOf(forecastOrderLine.getSubStatus())));
            forecastOrderLineSDO.setSubStatus(String.valueOf(forecastOrderLine.getSubStatus()));
            forecastOrderLineSDO.setSubStatusDesc(forecastStatusEnum!=null?forecastStatusEnum.getDesc():"");
        }
        if (forecastOrderLine.getMainStatus() != null) {
            ForecastStatusEnum forecastStatusEnum = ForecastStatusEnum.of(Integer.parseInt(String.valueOf(forecastOrderLine.getMainStatus())));
            forecastOrderLineSDO.setOrderStatus(String.valueOf(forecastOrderLine.getMainStatus()));
            forecastOrderLineSDO.setOrderStatusDesc(forecastStatusEnum!=null?forecastStatusEnum.getDesc():"");
        }
        forecastOrderLineSDO.setSaleUnit(forecastOrderLine.getSubSaleUnitcode());
        forecastOrderLineSDO.setSaleUnitName(forecastOrderLine.getSubSaleUnit());
        forecastOrderLineSDO.setInventoryUnit(forecastOrderLine.getSubInventoryUnitCode());
        forecastOrderLineSDO.setInventoryUnitName(forecastOrderLine.getSubInventoryUnit());
        forecastOrderLineSDO.setUnitConvert(forecastOrderLine.getSubUnitConvert());
        forecastOrderLineSDO.setQuantity(forecastOrderLine.getSubQuantity());
        forecastOrderLineSDO.setUnitPrice(forecastOrderLine.getSubUnitPrice()!=null?String.valueOf(forecastOrderLine.getSubUnitPrice()):null);
        if (!ObjectUtils.isEmpty(forecastOrderLine.getSubUnitPrice()) && !ObjectUtils.isEmpty(forecastOrderLine.getSubQuantity())) {
            Long l = forecastOrderLine.getSubUnitPrice() * Integer.parseInt(forecastOrderLine.getSubQuantity());
            forecastOrderLineSDO.setTotalFee(l.toString());
        }
        forecastOrderLineSDO.setActivityPrice(forecastOrderLine.getSubActivityPrice()!=null?String.valueOf(forecastOrderLine.getSubActivityPrice()):null);
        if (!ObjectUtils.isEmpty(forecastOrderLine.getSubActivityPrice()) && !ObjectUtils.isEmpty(forecastOrderLine.getSubQuantity())) {
            Long l = forecastOrderLine.getSubActivityPrice() * Integer.parseInt(forecastOrderLine.getSubQuantity());
            forecastOrderLineSDO.setActivityTotalAmount(l.toString());
        }
        forecastOrderLineSDO.setActualTotalReplyAmount(forecastOrderLine.getSubActualTotalReplyAmount()!=null?String.valueOf(forecastOrderLine.getSubActualTotalReplyAmount()):null);

        forecastOrderLineSDO.setActivityId(forecastOrderLine.getSubActivityCode());
        forecastOrderLineSDO.setActivityLineNo(forecastOrderLine.getSubActivityNumber());
        forecastOrderLineSDO.setActivityName(forecastOrderLine.getSubActivityName());
        forecastOrderLineSDO.setActivityPriceRemark(forecastOrderLine.getSubActivityPriceRemark());
        forecastOrderLineSDO.setCreateReasonCode(forecastOrderLine.getMainCreateReason());
        forecastOrderLineSDO.setCreateReasonName(forecastOrderLine.getMainCreateReasonName());
        forecastOrderLineSDO.setRefuseReasonCode(forecastOrderLine.getSubRefuseReasonCode());
        forecastOrderLineSDO.setRefuseReasonName(forecastOrderLine.getSubRefuseReason());
        forecastOrderLineSDO.setOrderRemark(forecastOrderLine.getMainRemark());
        forecastOrderLineSDO.setOrderLineRemark(forecastOrderLine.getSubDetailRemark());
        forecastOrderLineSDO.setShippingWarehouseCode(forecastOrderLine.getMainShippingWarehouseCode());
        forecastOrderLineSDO.setShippingWarehouseName(forecastOrderLine.getMainShippingWarehouseName());
        forecastOrderLineSDO.setSAPFactoryCode(forecastOrderLine.getMainDeliveryFactoryCode());
        forecastOrderLineSDO.setSAPInventoryPoint(forecastOrderLine.getMainStockCode());
        forecastOrderLineSDO.setDeliveryMethod(forecastOrderLine.getMainTransportModeCode());
        forecastOrderLineSDO.setDeliveryMethodName(forecastOrderLine.getMainTransportModeName());
        forecastOrderLineSDO.setFromSys(forecastOrderLine.getMainFromSys());
        forecastOrderLineSDO.setSourceOrderNo(forecastOrderLine.getMainSourceOrderNo());
        forecastOrderLineSDO.setSourceLineNum(forecastOrderLine.getSubSourceLineNum());
        forecastOrderLineSDO.setCreatorId(forecastOrderLine.getMainCreatorId());
        forecastOrderLineSDO.setCreatorName(forecastOrderLine.getMainCreatorName());
        forecastOrderLineSDO.setCreatorWorkId(forecastOrderLine.getMainCreatorCode());
        forecastOrderLineSDO.setUpdatorId(forecastOrderLine.getMainModifierId());
        forecastOrderLineSDO.setUpdatorName(forecastOrderLine.getMainModifierName());
        forecastOrderLineSDO.setUpdatorWorkId(forecastOrderLine.getMainModifierCode());

        //子单创建日期
        if (forecastOrderLine.getSubGmtCreate() != null) {
            forecastOrderLineSDO.setGmtCreateDate(DateUtils.toDateStr(forecastOrderLine.getSubGmtCreate(), DateUtils.DATETIME_FORMAT));
        }
        //子单修改日期
        if (forecastOrderLine.getSubGmtModified() != null) {
            forecastOrderLineSDO.setGmtModifiedDate(DateUtils.toDateStr(forecastOrderLine.getSubGmtModified(),DateUtils.DATETIME_FORMAT));
        }
        //客户期望交货日期
        if (forecastOrderLine.getMainCustomerExpectTDate() != null) {
            forecastOrderLineSDO.setCustomerExpectDate(DateUtils.toDateStr(forecastOrderLine.getMainCustomerExpectTDate(),DateUtils.DATE_FORMAT));
        }
        //客户采购订单日期
        if (forecastOrderLine.getMainCustomerPurchaseDate() != null) {
            forecastOrderLineSDO.setCustomerPurchaseDate(DateUtils.toDateStr(forecastOrderLine.getMainCustomerPurchaseDate(),DateUtils.DATE_FORMAT));
        }
        forecastOrderLineSDO.setCustomerReferenceNo(forecastOrderLine.getMainCustomerPurchaseOrderNo());
        forecastOrderLineSDO.setSyncVersion(forecastOrderLine.getSubSyncVersion()!=null?forecastOrderLine.getSubSyncVersion().intValue():null);

        //兼容订单平台字段
        if (StringUtils.isBlank(forecastOrderLineSDO.getCustomerCode()) && StringUtils.isNotBlank(forecastOrderLine.getMainCustomerCode())) {
            forecastOrderLineSDO.setCustomerCode(forecastOrderLine.getMainCustomerCode());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getCustomerName()) && StringUtils.isNotBlank(forecastOrderLine.getMainCustomerName())) {
            forecastOrderLineSDO.setCustomerName(forecastOrderLine.getMainCustomerName());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getReceiveAddressCode()) && StringUtils.isNotBlank(forecastOrderLine.getMainReceiveAddressCode())) {
            forecastOrderLineSDO.setReceiveAddressCode(forecastOrderLine.getMainReceiveAddressCode());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getReceiveDetailAddress()) && StringUtils.isNotBlank(forecastOrderLine.getMainReceiveDetailAddress())) {
            forecastOrderLineSDO.setReceiveDetailAddress(forecastOrderLine.getMainReceiveDetailAddress());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getBisUnitCode()) && StringUtils.isNotBlank(forecastOrderLine.getMainBisUnitCode())) {
            forecastOrderLineSDO.setBisUnitCode(forecastOrderLine.getMainBisUnitCode());
            forecastOrderLineSDO.setBisUnitName(null);
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getSalesOrganizationCode()) && StringUtils.isNotBlank(forecastOrderLine.getMainOrganizationCode())) {
            forecastOrderLineSDO.setSalesOrganizationCode(forecastOrderLine.getMainOrganizationCode());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getSalesOrganizationName()) && StringUtils.isNotBlank(forecastOrderLine.getMainOrganizationName())) {
            forecastOrderLineSDO.setSalesOrganizationName(forecastOrderLine.getMainOrganizationName());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getSalesChannelCode()) && StringUtils.isNotBlank(forecastOrderLine.getMainSaleChannelCode())) {
            forecastOrderLineSDO.setSalesChannelCode(forecastOrderLine.getMainSaleChannelCode());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getSalesChannelName()) && StringUtils.isNotBlank(forecastOrderLine.getMainSaleChannelName())) {
            forecastOrderLineSDO.setSalesChannelName(forecastOrderLine.getMainSaleChannelName());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getSalesDepartmentCode()) && StringUtils.isNotBlank(forecastOrderLine.getMainDepartmentCode())) {
            forecastOrderLineSDO.setSalesDepartmentCode(forecastOrderLine.getMainDepartmentCode());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getSalesDepartmentName()) && StringUtils.isNotBlank(forecastOrderLine.getMainDepartmentName())) {
            forecastOrderLineSDO.setSalesDepartmentName(forecastOrderLine.getMainDepartmentName());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getShippingWarehouseCode()) && StringUtils.isNotBlank(forecastOrderLine.getMainWareHouseCode())) {
            forecastOrderLineSDO.setShippingWarehouseCode(forecastOrderLine.getMainWareHouseCode());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getShippingWarehouseName()) && StringUtils.isNotBlank(forecastOrderLine.getMainWareHouseName())) {
            forecastOrderLineSDO.setShippingWarehouseName(forecastOrderLine.getMainWareHouseName());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getCustomerReferenceNo()) && StringUtils.isNotBlank(forecastOrderLine.getMainCustomerReferenceNo())) {
            forecastOrderLineSDO.setCustomerReferenceNo(forecastOrderLine.getMainCustomerReferenceNo());
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getOrderType()) && StringUtils.isNotBlank(forecastOrderLine.getMainOrderType())) {
            ForecastOrderTypeEnum forecastOrderTypeEnum = ForecastOrderTypeEnum.ofByValue(forecastOrderLine.getMainOrderType());
            if(forecastOrderTypeEnum != null){
                forecastOrderLineSDO.setOrderType(forecastOrderTypeEnum.getCode());
                forecastOrderLineSDO.setOrderTypeName(forecastOrderTypeEnum.getDesc());
            }
        }
        if (StringUtils.isBlank(forecastOrderLineSDO.getCreateReasonCode()) && StringUtils.isNotBlank(forecastOrderLine.getMainCreateReason())) {
            forecastOrderLineSDO.setCreateReasonCode(forecastOrderLine.getMainCreateReason());
            forecastOrderLineSDO.setCreateReasonName(null);
        }

        return forecastOrderLineSDO;
    }



}
